package com.tancire.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.exception.user.MemberErrorException;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.member.domain.Member;
import com.tancire.member.domain.MemberPurse;
import com.tancire.member.mapper.MemberMapper;
import com.tancire.member.service.IMemberPurseService;
import com.tancire.member.service.IMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员管理Service业务层处理
 *
 * @author tancire
 * @date 2024-05-20
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private IMemberPurseService memberPurseService;

    /**
     * 查询会员管理
     *
     * @param memberId 会员管理主键
     * @return 会员管理
     */
    @Override
    public Member selectMemberByMemberId(Long memberId) {
        return memberMapper.selectMemberByMemberId(memberId);
    }

    /**
     * 查询会员管理列表
     *
     * @param member 会员管理
     * @return 会员管理
     */
    @Override
    public List<Member> selectMemberList(Member member) {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员管理
     *
     * @param member 会员管理
     * @return 结果
     */
    @Override
    public int insertMember(Member member) {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员管理
     *
     * @param member 会员管理
     * @return 结果
     */
    @Override
    public int updateMember(Member member) {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员管理
     *
     * @param memberIds 需要删除的会员管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberByMemberIds(Long[] memberIds) {
        return memberMapper.deleteMemberByMemberIds(memberIds);
    }

    /**
     * 删除会员管理信息
     *
     * @param memberId 会员管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberByMemberId(Long memberId) {
        return memberMapper.deleteMemberByMemberId(memberId);
    }

    /**
     * 根据微信openid查询账号信息
     *
     * @param openid
     * @return
     */
    @Override
    public Member selectMemberByOpenId(String openid) {
        return memberMapper.selectMemberByOpenId(openid);
    }

    /**
     * 根据账号查询会员信息
     *
     * @param username
     * @return
     */
    @Override
    public Member selectMemberByAccount(String username) {
        return memberMapper.selectMemberByAccount(username);
    }

    /**
     * 获取会员详情
     *
     * @return
     */
    @Override
    public Member selectMemberByMemberInfo() {
        Long userId = SecurityUtils.getUserId();
        Member member = this.memberMapper.selectMemberByMemberId(userId);
        member.setPassword(null);
        member.setOpenId(null);
        member.setSessionKey(null);
        MemberPurse memberPurse = this.memberPurseService.selectMemberPurseByMemberId(userId);
        member.setMemberPurse(memberPurse);
        return member;
    }

    /**
     * 校验账号名重复
     *
     * @return
     */
    @Override
    public boolean checkAccountUnique(String account) {
        int count = this.memberMapper.selectCountByAccount(account);
        return count > 0;
    }

    /**
     * 更新个人账号信息
     *
     * @param member
     */
    @Override
    public void updateMemberInfo(Member member) {
        Long userId = SecurityUtils.getUserId();
        if (!userId.equals(member.getMemberId())) {
            throw new MemberErrorException("非法请求");
        }
        Member updateMember = new Member();
        updateMember.setMemberId(member.getMemberId());
        updateMember.setNickName(member.getNickName());
        updateMember.setAvatar(member.getAvatar());
        updateMember.setPhoneNumber(member.getPhoneNumber());
        this.memberMapper.updateMember(updateMember);
    }

    /**
     * 查询排名前20人
     *
     * @return
     */
    @Override
    public List<Member> selectMemberTopLevelList() {
        return this.memberMapper.selectMemberTopLevelList();
    }

    /**
     * 根据IDS查询会员信息
     *
     * @param ids
     * @return
     */
    @Override
    public List<Member> selectMemberByIds(List<Long> ids) {
        return this.memberMapper.selectMemberByIds(ids);
    }

    /**
     * 获取会员id和会员名map
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Long, String> selectMemberMapByIds(List<Long> ids) {
        // 初始化map
        Map<Long, String> map = new HashMap<>();
        // 判空
        if (ObjectUtil.isNotEmpty(ids)) {
            List<Member> members = this.memberMapper.selectMemberByIds(ids);
            if (ObjectUtil.isNotEmpty(members)) {
                for (Member member : members) {
                    map.put(member.getMemberId(), member.getNickName());
                }
            }
        }
        return map;
    }
}
